package com.example.myapplication;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

//修复生产环境的问题
public class MainActivity extends AppCompatActivity {
    public int Sign_RequestCode = 1;
    Button sign;
    Button login;
    EditText name;
    EditText psw;
    private UserDBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        sign = findViewById(R.id.sign);
        login = findViewById(R.id.login);
        name = findViewById(R.id.name);
        psw = findViewById(R.id.psw);
        dbHelper = new UserDBHelper(this);

        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, Sign.class);
                startActivityForResult(intent, Sign_RequestCode);
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = name.getText().toString();
                String password = psw.getText().toString();

                SQLiteDatabase db = dbHelper.getReadableDatabase();
                String[] projection = {UserDBHelper.COLUMN_PASSWORD};
                String selection = UserDBHelper.COLUMN_NAME + " = ?";
                String[] selectionArgs = {username};

                Cursor cursor = db.query(UserDBHelper.TABLE_NAME, projection, selection, selectionArgs, null, null, null);

                if (cursor.moveToFirst()) {
                    int passwordIndex = cursor.getColumnIndex(UserDBHelper.COLUMN_PASSWORD);
                    String storedPassword = cursor.getString(passwordIndex);

                    if (password.equals(storedPassword)) {
                        Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent();
                        intent.setClass(MainActivity.this, MainActivity_home.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(MainActivity.this, "登录失败，密码或账号错误", Toast.LENGTH_SHORT).show();//登录成功则弹出弹窗
                    }
                } else {
                    Toast.makeText(MainActivity.this, "登录失败，密码或账号错误", Toast.LENGTH_SHORT).show();
                }//登录失败则弹出密码错误弹窗

                cursor.close();
                db.close();
            }
        });
    }
//修复发布版本不能正确的登录问题
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Sign_RequestCode) {
            if (resultCode == RESULT_OK) {
                String name1 = data.getStringExtra("name");
                String psw1 = data.getStringExtra("psw");
                name.setText(name1);
                psw.setText(psw1);
            }
        }
    }
}
